
//适配类型
var FitSceneType = cc.Enum({
    ExactFit: 0,
    NoBorder: 1,
    ShowAll: 2,
    FixedHeight: 3,
    FixedWidth: 4,
    Auto: 5
});

let config = require('Config');
let logger = require('Log');

cc.Class({
    extends: cc.Component,

    properties: {
        fitSceneType: {
            default: FitSceneType.None,
            type: FitSceneType
        }
    },

    // use this for initialization
    onLoad: function () {

        //指定适配
        if (this.fitSceneType != FitSceneType.Auto)
            cc.view.setDesignResolutionSize(config.designSize.width, config.designSize.height, this.fitSceneType);
        else {
            /**
             * 自动适配方案
             * 
             * 1.从项目配置中获取 设计尺寸 DesignSize，计算出设计宽高比 D_WH_Scale
             * 
             * 2.获取屏幕尺寸 WinSize，求出宽高比例 WH_Scale
             * 
             * 3.计算 D_WH_Scale 与 WH_Scale 的比例因子 scale
             * 
             * 4.根据 scale 的范围，调整适配方案，默认为 ExactFit 与 ShowAll 两种
             * 
             */

            //设计尺寸 
            let designSize = config.designSize;
            //屏幕尺寸
            let winSize = cc.director.getWinSize();

            //设计比例
            let D_WH_Scale = config.designSize.width / config.designSize.height;
            //屏幕比例
            let WH_Scale = winSize.width / winSize.height;

            logger.log("============>>designSize.width:" + designSize.width + ",designSize.height:" + designSize.height);
            logger.log("============>>winSize.width:" + winSize.width + ",winSize.height:" + winSize.height);
            logger.log("============>>D_WH_Scale:" + D_WH_Scale + ",WH_Scale:" + WH_Scale);

            //缩放因子
            let scale = D_WH_Scale / WH_Scale;

            logger.log("============>>scale:" + scale);

            if (scale >= 0.8 && scale <= 1.3) {
                cc.view.setDesignResolutionSize(config.designSize.width, config.designSize.height, FitSceneType.ExactFit);
            } else if (scale > 1.3 || scale < 0.8) {
                cc.view.setDesignResolutionSize(config.designSize.width, config.designSize.height, FitSceneType.ShowAll);
            }
        }
    },
});
